{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "cfe19aea-4c78-4b23-9f9a-53f360819505",
   "metadata": {},
   "outputs": [],
   "source": [
    "from transformers import pipeline, BertForQuestionAnswering, BertTokenizer\n",
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c473aff1-2234-4abc-a0d2-79a0d91fdf10",
   "metadata": {},
   "outputs": [],
   "source": [
    "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
    "qa_model = BertForQuestionAnswering.from_pretrained('bert-large-uncased-whole-word-masking-finetuned-squad', device_map=device)\n",
    "qa_tokenizer = BertTokenizer.from_pretrained('bert-large-uncased-whole-word-masking-finetuned-squad', device=device)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2a4a0cc8-b1e1-4127-9d1a-747ec44b6ef0",
   "metadata": {},
   "outputs": [],
   "source": [
    "question_answer_pipeline = pipeline(\"question-answering\", model=qa_model, tokenizer=qa_tokenizer)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "8f7eb866-49c4-4036-b38e-93120869e629",
   "metadata": {},
   "outputs": [],
   "source": [
    "context = \"The cat had no business entering the neighbors garage, but she was there to help. The neighbor, who asked not to be identified, said she didn't know what to make of the cat's behavior. She said it seemed like it was trying to get into her home, and that she was afraid for her life. The neighbor said that when she went to check on her cat, it ran into the neighbor's garage and hit her in the face, knocking her to the ground.\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "27eedc51-62f6-4b2b-95ec-76bb28775352",
   "metadata": {},
   "outputs": [],
   "source": [
    "question = \"Where was the cat trying to enter?\"\n",
    "result = question_answer_pipeline(question=question, context=context)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c03eacb5-0aea-4f00-bb14-3a459b76773f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'score': 0.2555047571659088, 'start': 33, 'end': 54, 'answer': 'the neighbors garage,'}\n"
     ]
    }
   ],
   "source": [
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "cb992c91-ab6d-432a-b322-98afc2b1fcf0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "the neighbors garage,\n"
     ]
    }
   ],
   "source": [
    "print(result['answer'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3fc7c4a2-b1b4-4544-baf5-a3b57bf02075",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hit her in the face, knocking her to the ground.\n"
     ]
    }
   ],
   "source": [
    "question = \"What did the cat do after entering the garage\"\n",
    "result = question_answer_pipeline(question=question, context=context)\n",
    "print(result['answer'])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "chapter09",
   "language": "python",
   "name": "chapter09"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
